/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "lvgl.h"
#include <stdio.h>
#include "gui_guider.h"
#include "events_init.h"
#include "widgets_init.h"
#include "nrf_process.h"
#include "custom.h"


// 图标数量
#define icon_count          2
// 图标边缘间距（推荐20-30）
#define ICON_EDGE_GAP       20
// 小图标的尺寸
#define icon_size_small     70
// 大图标的尺寸
#define icon_size_big       120

// 定义图标结构体
typedef struct {
  lv_obj_t * obj;
  int32_t x;
} icon_typedef;

// 声明图标结构体数组
static icon_typedef icon[icon_count];


static lv_obj_t * imgs[icon_count];        // img arrary
static uint8_t imgsName[icon_count][20];  // Picture name arrary
// 当前焦点图片索引
static int current_index = icon_count/2;

// 当前选中图标索引
static int selected_idx = 0;

// 图标切换函数
void scroll_light_1_icon_select(int dir, lv_ui *ui)
{
    // 保存之前选中的索引
    int prev_selected_idx = selected_idx;

    // 更新选中的图标索引
    if (dir == 0 && selected_idx > 0) selected_idx--;
    else if (dir == 1 && selected_idx < icon_count - 1) selected_idx++;
    else {
        if (!selected_idx) {
            selected_idx = icon_count - 1;
        } else {
            selected_idx = 0;
        }
    }

    // 获取当前选中图标的名称
    char *selected_icon_name = (char*)imgsName[selected_idx];
    lv_label_set_text(guider_ui.light_1_label_1, selected_icon_name);

    // 更新所有图标的大小和可见性
    for (int i = 0; i < icon_count; i++) {
        if (i == selected_idx) {
            lv_obj_set_size(icon[i].obj, icon_size_big, icon_size_big);
            lv_obj_clear_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
        } else {
            lv_obj_set_size(icon[i].obj, icon_size_small, icon_size_small);
            lv_obj_add_flag(icon[i].obj, LV_OBJ_FLAG_HIDDEN);
        }
        lv_obj_align(icon[i].obj, LV_ALIGN_CENTER, 0, 0);
    }

    // 设置焦点到新选中的图标（修正函数调用）
    if (icon[selected_idx].obj) {
        lv_group_focus_obj(icon[selected_idx].obj); // LVGL 8.3只需要一个参数

        // 确保界面刷新
        lv_obj_invalidate(icon[selected_idx].obj);
        lv_obj_invalidate(icon[prev_selected_idx].obj);
    }
}
// 图标按下事件回调
void light_on_off_pressed_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code) {
    case LV_EVENT_PRESSED:
    {
        nrf_para.nrf.start_send_flag = true;
        nrf_para.nrf.send_cmd = LIGHT_CMD;
        break;
    }

    default:
        break;
    }
}



void setup_scr_light_1(lv_ui *ui)
{
    lv_group_t * main_group = lv_group_create();//创建一个组
    lv_indev_set_group(indev_keypad, main_group);

    //Write codes massage_onoff
    ui->light_1 = lv_obj_create(NULL);
    lv_obj_set_size(ui->light_1, 240, 320);
    lv_obj_set_scrollbar_mode(ui->light_1, LV_SCROLLBAR_MODE_OFF);

    //Write style for massage_onoff, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_bg_opa(ui->light_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->light_1, lv_color_hex(guider_ui.color.backlight), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->light_1, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);

    //Write codes massage_onoff_btn_1
    ui->light_1_btn_1 = lv_btn_create(ui->light_1);
    ui->light_1_label_1 = lv_label_create(ui->light_1_btn_1);
    if (backlight.bits.led) {
        lv_label_set_text(ui->light_1_label_1, "OFF");
    }  else {
        lv_label_set_text(ui->light_1_label_1, "ON");
    }
    lv_label_set_long_mode(ui->light_1_label_1, LV_LABEL_LONG_WRAP);
    lv_obj_align(ui->light_1_label_1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_set_style_pad_all(ui->light_1_btn_1, 0, LV_STATE_DEFAULT);
    lv_obj_set_width(ui->light_1_label_1, LV_PCT(100));
    lv_obj_set_pos(ui->light_1_btn_1, 68, 111);
    lv_obj_set_size(ui->light_1_btn_1, 99, 99);
    lv_obj_add_flag(ui->light_1_btn_1, LV_OBJ_FLAG_CLICK_FOCUSABLE);

    //Write style for massage_onoff_btn_1, Part: LV_PART_MAIN, State: LV_STATE_DEFAULT.
    lv_obj_set_style_bg_opa(ui->light_1_btn_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui->light_1_btn_1, lv_color_hex(0x1f5f94), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_bg_grad_dir(ui->light_1_btn_1, LV_GRAD_DIR_NONE, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_border_width(ui->light_1_btn_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui->light_1_btn_1, 5, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_shadow_width(ui->light_1_btn_1, 0, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(ui->light_1_btn_1, lv_color_hex(0xffffff), LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui->light_1_btn_1, &lv_font_montserrat_14, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui->light_1_btn_1, 255, LV_PART_MAIN|LV_STATE_DEFAULT);
    lv_obj_set_style_text_align(ui->light_1_btn_1, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN|LV_STATE_DEFAULT);

    lv_group_add_obj(main_group, ui->light_1_btn_1);
    //The custom code of massage_onoff.
    lv_obj_add_event_cb(ui->light_1_btn_1, light_on_off_pressed_cb, LV_EVENT_ALL, ui);

    //Update current screen layout.
    lv_obj_update_layout(ui->light_1);

}
